using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;

namespace RunAliasCreator
{
	[RunInstaller(true)]
	public partial class RunAliasCreatorInstaller : Installer
	{
		public RunAliasCreatorInstaller()
		{
			InitializeComponent();
		}

		public override void Install(System.Collections.IDictionary stateSaver)
		{
			base.Install(stateSaver);
			stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());
		}

		public override void Commit(System.Collections.IDictionary savedState)
		{
			base.Commit(savedState);
			string targetDir = savedState["TargetDir"].ToString();
			string targetFile;
			bool windows7 = Environment.OSVersion.Version > new Version(6, 1);
			string createRunAlias = windows7 ? "Create run alias" : "Create Run Alias";
			string findTarget = windows7 ? "Find target" : "Find Target";
			string copyPath = windows7 ? "Copy path" : "Copy Path";
			try
			{
				targetFile = Path.Combine(targetDir, "Run Alias Creator.exe");
				RunAlias.Create(targetFile, "RunAlias", true);
				ExplorerContextMenu.CreateCommand(null, createRunAlias, "\"" + targetFile + "\" \"%1\"", true);
				ExplorerContextMenu.CreateCommand("Folder", createRunAlias, "\"" + targetFile + "\" \"%1\"", true);
				targetFile = Path.Combine(targetDir, "FindTarget.exe");
				RunAlias.Create(targetFile, "Find", true);
				if (!windows7)
				{
					ExplorerContextMenu.CreateCommand(null, findTarget, "\"" + targetFile + "\" \"%1\"", true);
					ExplorerContextMenu.CreateCommand("Folder", findTarget, "\"" + targetFile + "\" \"%1\"", true);
				}
				ExplorerContextMenu.CreateCommand(null, copyPath, "\"" + targetFile + "\" /c \"%1\"", true);
				ExplorerContextMenu.CreateCommand("Folder", copyPath, "\"" + targetFile + "\" /c \"%1\"", true);
				targetFile = Path.Combine(targetDir, "Copy.lnk");
				RunAlias.Create(targetFile, "Copy", true);
			}
			catch { }
		}

		protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
		{
			base.OnBeforeUninstall(savedState);
			try
			{
				RunAlias.Delete("RunAlias");
				ExplorerContextMenu.DeleteCommand(null, "Create Run Alias");
				ExplorerContextMenu.DeleteCommand("Folder", "Create Run Alias");
				ExplorerContextMenu.DeleteCommand(null, "Find Target");
				ExplorerContextMenu.DeleteCommand(null, "Copy Path");
				ExplorerContextMenu.DeleteCommand("Folder", "Find Target");
				ExplorerContextMenu.DeleteCommand("Folder", "Copy Path");
			}
			catch { }
		}

	}
}